<?php

/**
 *    商品展示页面控制器
 *
 *    @author   yanghj 2014.6.47
 *    @usage    none
 */
class GoodsApp extends StorebaseApp
{
	var $_goods_mod;
	function __construct()
	{
		$this->GoodsApp();
	}
	function GoodsApp()
	{
		parent::__construct();
		$this->_goods_mod =& m('goods');
	}

	function index()
	{

		/* 参数 id */
		$id = empty($_GET['id']) ? 0 : intval($_GET['id']);
		if (!$id)
		{
			$this->show_warning('Hacking Attempt');
			return;
		}

		/* 可缓存数据 */
		$data = $this->_get_common_info($id);
		if ($data === false)
		{
			return;
		}
		else
		{
			$this->_assign_common_info($data);
		}

		/* 更新浏览次数 */
		$this->_update_views($id);

		//是否开启验证码
		if (Conf::get('captcha_status.goodsqa'))
		{
			$this->assign('captcha', 1);
		}

		$this->assign("comment_count", count($this->_get_goods_comment($id)));
		$this->assign('guest_comment_enable', Conf::get('guest_comment'));
		$this->assign('goods_label', $this->_get_goods_label($data['goods']));//商品标签
		$this->assign('store_gcategory_nav', $this->_get_store_gcategory_nav());//获取导航商品分类
		$this->assign('app_name', "goods");
		$this->display('goods.index.html');
	}

	/* 商品评论 */
	function comments()
	{
		$id = empty($_GET['id']) ? 0 : intval($_GET['id']);
		if (!$id)
		{
			$this->json_error('没有相关的评论',array());
			return;
		}

		/* 赋值商品评论 */
		$data = $this->_get_goods_comment($id);
		if($data)
		{
			$data = array_values($data);
			$this->json_result($data, '请求成功!');
		}
		else
		{
			$this->json_error('没有相关的评论',array());
			return;
		}


	}

	/* 销售记录 */
	function saleslog()
	{
		$id = empty($_GET['id']) ? 0 : intval($_GET['id']);
		if (!$id)
		{
			$this->show_warning('Hacking Attempt');
			return;
		}

		$data = $this->_get_common_info($id);
		if ($data === false)
		{
			return;
		}
		else
		{
			$this->_assign_common_info($data);
		}

		/* 赋值销售记录 */
		$data = $this->_get_sales_log($id, 10);
		$this->_assign_sales_log($data);

		$this->display('goods.saleslog.html');
	}
	function qa()
	{
		$goods_qa =& m('goodsqa');
		$id = intval($_GET['id']);
		if (!$id)
		{
			$this->show_warning('Hacking Attempt');
			return;
		}
		if(!IS_POST)
		{
			$data = $this->_get_common_info($id);
			if ($data === false)
			{
				return;
			}
			else
			{
				$this->_assign_common_info($data);
			}
			$data = $this->_get_goods_qa($id, 10);
			$this->_assign_goods_qa($data);

			//是否开启验证码
			if (Conf::get('captcha_status.goodsqa'))
			{
				$this->assign('captcha', 1);
			}
			$this->assign('guest_comment_enable', Conf::get('guest_comment'));
			/*赋值产品咨询*/
			$this->display('goods.qa.html');
		}
		else
		{
			/* 不允许游客评论 */
			if (!Conf::get('guest_comment') && !$this->visitor->has_login)
			{
				$this->show_warning('guest_comment_disabled');

				return;
			}
			$content = (isset($_POST['content'])) ? trim($_POST['content']) : '';
			//$type = (isset($_POST['type'])) ? trim($_POST['type']) : '';
			$email = (isset($_POST['email'])) ? trim($_POST['email']) : '';
			$hide_name = (isset($_POST['hide_name'])) ? trim($_POST['hide_name']) : '';
			if (empty($content))
			{
				$this->show_warning('content_not_null');
				return;
			}
			//对验证码和邮件进行判断

			if (Conf::get('captcha_status.goodsqa'))
			{
				if (base64_decode($_SESSION['captcha']) != strtolower($_POST['captcha']))
				{
					$this->show_warning('captcha_failed');
					return;
				}
			}
			if (!empty($email) && !is_email($email))
			{
				$this->show_warning('email_not_correct');
				return;
			}
			$user_id = empty($hide_name) ? $_SESSION['user_info']['user_id'] : 0;
			$conditions = 'g.goods_id ='.$id;
			$goods_mod = & m('goods');
			$ids = $goods_mod->get(array(
			'fields' => 'store_id,goods_name',
			'conditions' => $conditions
			));
			extract($ids);
			$data = array(
			'question_content' => $content,
			'type' => 'goods',
			'item_id' => $id,
			'item_name' => addslashes($goods_name),
			'store_id' => $store_id,
			'email' => $email,
			'user_id' => $user_id,
			'time_post' => gmtime(),
			);
			if ($goods_qa->add($data))
			{
				header("Location: index.php?app=goods&act=qa&id={$id}#module\n");
				exit;
			}
			else
			{
				$this->show_warning('post_fail');
				exit;
			}
		}
	}
	function get_shipping_fee()
	{
		$shipping_id = isset($_GET['shipping_id']) ? intval($_GET['shipping_id']) : 0;
		$goods_weight = isset($_GET['goods_weight']) ? floatval($_GET['goods_weight']) : 0;
		$goods_number = isset($_GET['goods_number']) ? intval($_GET['goods_number']) : 0;
		$region_id = isset($_GET['region_id']) ? intval($_GET['region_id']) : 0;
		if(!$shipping_id || !$goods_number || !$region_id)
		{
			$this->json_error('数据不完整,无法计算运费');
			return;
		}
		$shipping_mod =& m('shipping');
		$shipping_fee = $shipping_mod->shipping_fee($shipping_id, $goods_weight*$goods_number, 0, $goods_number,$region_id);
		$this->json_result($shipping_fee, '计算成功');//返回运费总数
	}



	/**
     * 取得公共信息
     *
     * @param   int     $id
     * @return  false   失败
     *          array   成功
     */
	function _get_common_info($id)
	{
		$cache_server =& cache_server();
		$key = 'page_of_goods_' . $id;
		$data = $cache_server->get($key);
		//$shipping_mod =& m('shipping');
		$cached = true;
		if ($data === false || !isset($data['goods']))
		{
			$cached = false;
			$data = array('id' => $id);

			/* 商品信息 */
			$goods = $this->_goods_mod->get_info($id);

			if (!$goods || $goods['if_show'] == 0 || $goods['closed'] == 1 || $goods['state'] != 1 )
			{
				$this->show_warning('很抱歉，您查看的商品不存在','back_index', 'index.php');
				return false;
			}
			$goods['tags'] = $goods['tags'] ? explode(',', trim($goods['tags'], ',')) : array();
			if($goods['shipping_id'])
			{
				$shipping_mod =& m('shipping');
				$goods['cod_regions'] = $shipping_mod->get_fields_name($goods['shipping_id'],'cod_regions');
				$goods['cod_regions'] = $this->_rebuild_region($goods['cod_regions']);
			}
            $goods['price_interval'] = $this->_goods_mod->get_price_interval($goods['goods_id'], $goods['price_type']);//获取商品的价格区间

			$data['goods'] = $goods;
            $data['goods']['is_promote'] = 0;

            /* 是否参加优惠活动 */
            if($goods['act_id'] > 0)
            {
                $sql = "SELECT ag.promote_quantity,ag.promote_discount,ag.promote_price,ag.act_type,a.act_name,a.start_time,a.end_time,a.discount_type,a.free_shipping FROM " .DB_PREFIX . "activity_goods AS ag left join " .DB_PREFIX . "activity AS a on ag.act_id = a.act_id WHERE ag.goods_id = {$goods['goods_id']}";
                $activity = $this->_goods_mod->getRow($sql);

                if(gmtime() > $activity['start_time'] && gmtime() < $activity['end_time'])
                {
                    $data['activity'] = $activity;
                    $data['goods']['is_promote'] = 1;
                    $data['goods']['life_time'] = $activity['end_time'] - gmtime();
                }
            }

			/* 店铺信息 */
			if (!$goods['store_id'])
			{
				$this->show_warning('该店铺没有这个商品','back_index', 'index.php');
				return false;
			}
			$this->set_store($goods['store_id']);
			$data['store_data'] = $this->get_store_data();
			$data['related_data'] = $this->_get_related_data($id,$goods['store_id']);

			/* 当前位置 */
			$data['cur_local'] = $this->_get_curlocal($goods['cate_id']);
			$data['goods']['related_info'] = $this->_get_related_objects($data['goods']['tags']);
			$data['region'] = $this->_get_region_list();//地区列表
			/* 分享链接 */
			//$data['share'] = $this->_get_share($goods);

			$cache_server->set($key, $data, 300);
		}
		if ($cached)
		{
			$this->set_store($data['goods']['store_id']);
		}

		return $data;
	}

	function _get_related_objects($tags)
	{
		if (empty($tags))
		{
			return array();
		}
		$tag = $tags[array_rand($tags)];
		$ms =& ms();

		return $ms->tag_get($tag);
	}

	/* 赋值公共信息 */
	function _assign_common_info($data)
	{
		include(ROOT_PATH . '/includes/cfg_status.php');
		/* 商品信息 */
		$goods = $data['goods'];
        $activity = $data['activity'];
        $goods['description'] = str_replace('_.webp','',$goods['description']);
		$goods_price_type = 1;
		if($goods['price_type'] == 1)
		{
			if($goods['spec_qty'] == 0)
			{
				$goods_price_type = 1;//数量报价,没有规格
			}
			if($goods['spec_qty'] == 1)
			{
				$goods_price_type = 2;//数量报价,一种规格
			}
			if($goods['spec_qty'] == 2)
			{
				$goods_price_type = 3;//数量报价,二种规格
			}
			/*如果是数量报价就重新封装价格区间*/
			foreach($goods['_vols'] as $key=>$val)
			{
				$goods['_vols'][$key]['min_number'] = $val['volume_number'];
				if(count($goods['_vols'])>1 && $key !=(count($goods['_vols'])-1))
				{
					$goods['_vols'][$key]['max_number'] = $goods['_vols'][$key+1]['volume_number']-1;
				}
                //如果是参与优惠活动的商品,则获取商品促销价格
                if($goods['is_promote'] == 1)
                {
                    //优惠方式为打折
                    if($activity['discount_type'] == 1)
                    {
                        $goods['_vols'][$key]['retail_price'] = $goods['_vols'][$key]['volume_price'];//原价格
                        $goods['_vols'][$key]['volume_price'] = sprintf("%.2f",$goods['_vols'][$key]['volume_price']*$activity['promote_discount']);//促销价格
                    }
                    //优惠方式为一口价
                    if($activity['discount_type'] == 2)
                    {
                        $goods['_vols'][$key]['retail_price'] = $goods['_vols'][$key]['volume_price'];//原价格
                        $goods['_vols'][$key]['volume_price'] = $activity['promote_price'];//促销价格
                    }
                }
			}
		}
		if($goods['price_type'] == 2)
		{
			if($goods['spec_qty'] == 1)
			{
				$goods_price_type = 4;//规格报价,一种规格
			}
			if($goods['spec_qty'] == 2)
			{
				$goods_price_type = 5;//规格报价,二种规格
			}
            foreach($goods['_specs'] as $key=>$val)
            {
                //如果是参与优惠活动的商品,则获取商品促销价格
                if($goods['is_promote'] == 1)
                {
                    //优惠方式为打折
                    if($activity['discount_type'] == 1)
                    {
                        $goods['_specs'][$key]['retail_price'] = $goods['_specs'][$key]['price'];//原价格
                        $goods['_specs'][$key]['price'] = sprintf("%.2f",$goods['_specs'][$key]['price']*$activity['promote_discount']);//促销价格
                    }
                    //优惠方式为一口价
                    if($activity['discount_type'] == 2)
                    {
                        $goods['_specs'][$key]['retail_price'] = $goods['_specs'][$key]['price'];//原价格
                        $goods['_specs'][$key]['price'] = $activity['promote_price'];//促销价格
                    }
                }
            }
		}
		if($goods_price_type == 3 || $goods_price_type == 5)
		{
			foreach($goods['_specs'] as $key=>$val)
			{
				$spec[$val['spec_1']][$key] = $val;
			}
			sort($spec);
			$goods['_specs'] = $spec;
		}
		if($goods['_attrs'])
		{
			$attr_arr = array();
			foreach($goods['_attrs'] as $key=>$val)
			{
				$temp_data[$val['attr_id']][$key]['gattr_id']   = $val['gattr_id'];
				$temp_data[$val['attr_id']][$key]['attr_name']  = $val['attr_name'];
				$temp_data[$val['attr_id']][$key]['attr_value'] = $val['attr_value'];
				$temp[$val['attr_id']][$key] = $val['attr_value'];
			}
			foreach($temp_data as $key=>$val)
			{
				foreach($val as $k => $v)
				{
					$attr_arr[$key]['gattr_id']   = $v['gattr_id'];
					$attr_arr[$key]['attr_name']  = $v['attr_name'];
					$attr_arr[$key]['attr_value'] = implode(' ',$temp[$key]);
				}
			}
		}

        //如果是参与优惠活动的商品,则重新组装价格区间
        if($goods['is_promote'] == 1)
        {
            //优惠方式为打折
            if($activity['discount_type'] == 1)
            {
                if(isset($goods['price_interval']['price']))
                {
                    $goods['price_interval']['retail_price'] = $goods['price_interval']['price'];//原价格
                    $goods['price_interval']['price'] = sprintf("%.2f",$goods['price_interval']['price']*$activity['promote_discount']);//促销价格
                }
                else
                {
                    $goods['price_interval']['retail_min_price'] = $goods['price_interval']['min_price'];//原最小价格
                    $goods['price_interval']['min_price'] = sprintf("%.2f",$goods['price_interval']['min_price']*$activity['promote_discount']);//促销最小价格
                    $goods['price_interval']['retail_max_price'] = $goods['price_interval']['max_price'];//原最大价格
                    $goods['price_interval']['max_price'] = sprintf("%.2f",$goods['price_interval']['max_price']*$activity['promote_discount']);//促销最大价格
                }
            }
            //优惠方式为一口价
            if($activity['discount_type'] == 2)
            {
                if(isset($goods['price_interval']['price']))
                {
                    $goods['price_interval']['retail_price'] = $goods['price_interval']['price'];//原价格
                }
                else
                {
                    $goods['price_interval']['retail_min_price'] = $goods['price_interval']['min_price'];//原最小价格
                    $goods['price_interval']['retail_max_price'] = $goods['price_interval']['max_price'];//原最大价格
                }
                $goods['price_interval']['price'] = $activity['promote_price'];//促销价格
            }
        }

        //与商品相关的数据
        $related_data = $data['related_data'];
        if($related_data['same_category_goods'])
        {
            $related_data['same_category_goods_view'] = array_slice($related_data['same_category_goods'],0,20);
            shuffle($related_data['same_category_goods']);
            shuffle($related_data['same_category_goods_view']);
            $related_data['same_category_goods_rand4'] = array_slice($related_data['same_category_goods'],0,4);//同一分类商品，从全部中随机抽取4个商品
            shuffle($related_data['same_category_goods']);
            $related_data['same_category_goods_rand6'] = array_slice($related_data['same_category_goods'],0,6);//商品下架后同一分类下的随机6个商品
            $related_data['same_category_goods_view_list'] = array_slice($related_data['same_category_goods_view'],0,5);//店铺同一分类，最近被其他人看过的商品
        }

        if($related_data['out_category_goods'])
        {
            shuffle($related_data['out_category_goods']);
            $related_data['out_category_goods_view_list'] = array_slice($related_data['out_category_goods'],0,5);//店铺不同分类，最近被其他人看过的商品
        }

		$goods['_attrs'] = $attr_arr;
		$this->assign('goods', $goods);
        $this->assign('activity', $activity);
		$this->assign('shipping_type', $cfg_shipping_type);
		$this->assign('shipping_time', $cfg_shipping_time);
		$this->assign('goods_price_type', $goods_price_type);
		$this->assign('region_html', $data['region']);//地区的省份与城市
		$this->assign('related_data', $related_data);//与商品相关的数据
		$this->assign('sales_info', sprintf(LANG::get('sales'), $goods['sales'] ? $goods['sales'] : 0));
		$this->assign('comments', sprintf(LANG::get('comments'), $goods['comments'] ? $goods['comments'] : 0));

		/* 店铺信息 */
		$data['store_data']['intro'] =  sub_str(strip_tags(str_replace('&nbsp;','',$data['store_data']['description'])), 70, true);
		$this->assign('store', $data['store_data']);

		//$this->assign('fh_img', $this->_get_store_fh_img($data['store_data']['store_id']));//过年春节发货通知图片
		$this->assign('showroom_img', $this->_get_store_showroom_img($data['store_data']['store_id']));//店铺展厅图片

		/* 浏览历史 */
		$this->assign('goods_history', $this->_get_goods_history($data['id']));

		/* 默认图片 */
		$this->assign('default_image', Conf::get('default_goods_image'));

		/* 当前位置 */
		$this->_curlocal($data['cur_local']);

		/* 配置seo信息 */
		$this->_config_seo($this->_get_seo_info($data['goods']));

		/* 商品分享 */
		//$this->assign('share', $data['share']);

		$this->import_resource(array(
		'script' => 'jquery.jqzoom.js',
		'style' => 'res:jqzoom.css'
		));
	}

	/* 取得浏览历史 */
	function _get_goods_history($id, $num = 10)
	{
		$goods_list = array();
		$goods_ids  = ecm_getcookie('goodsBrowseHistory');
		$goods_ids  = $goods_ids ? explode(',', $goods_ids) : array();
		if ($goods_ids)
		{
			$rows = $this->_goods_mod->find(array(
			'conditions' => $goods_ids,
			'fields'     => 'goods_name,default_image',
			));
			foreach ($goods_ids as $goods_id)
			{
				if (isset($rows[$goods_id]))
				{
					empty($rows[$goods_id]['default_image']) && $rows[$goods_id]['default_image'] = Conf::get('default_goods_image');
					$goods_list[] = $rows[$goods_id];
				}
			}
			krsort($goods_list);
		}
		$goods_ids[] = $id;
		if (count($goods_ids) > $num)
		{
			unset($goods_ids[0]);
		}
		ecm_setcookie('goodsBrowseHistory', join(',', array_unique($goods_ids)),time()+7*24*60*60);

		return $goods_list;
	}

	/* 取得销售记录 */
	function _get_sales_log($goods_id, $num_per_page)
	{
		$data = array();

		$page = $this->_get_page($num_per_page);
		$order_goods_mod =& m('ordergoods');
		$sales_list = $order_goods_mod->find(array(
		'conditions' => "goods_id = '$goods_id' AND status = '" . ORDER_FINISHED . "'",
		'join'  => 'belongs_to_order',
		'fields'=> 'buyer_id, buyer_name, add_time, anonymous, goods_id, specification, price, quantity, evaluation',
		'count' => true,
		'order' => 'add_time desc',
		'limit' => $page['limit'],
		));
		$data['sales_list'] = $sales_list;

		$page['item_count'] = $order_goods_mod->getCount();
		$this->_format_page($page);
		$data['page_info'] = $page;
		$data['more_sales'] = $page['item_count'] > $num_per_page;

		return $data;
	}

	/* 赋值销售记录 */
	function _assign_sales_log($data)
	{
		$this->assign('sales_list', $data['sales_list']);
		$this->assign('page_info',  $data['page_info']);
		$this->assign('more_sales', $data['more_sales']);
	}

	/* 取得商品评论 */
	function _get_goods_comment($goods_id)
	{
		$cache_server =& cache_server();
		$key = 'page_of_goods_comment_' . $goods_id;
		$data = $cache_server->get($key);
		if($data === false)
		{
			$comment_mod =& m('comment');
			$data = $comment_mod->find(array(
			'conditions' => "goods_id = '$goods_id' AND is_valid = '1' AND comment_type = '0'",
			'fields'=> 'from_user_id, from_user_name, evaluation, comment, add_time, quantity',
			'order' => 'add_time desc',
			));
			foreach($data as $key => $v)
			{
				$str = '';
				for($i=1;$i<=5;$i++)
				{
					if($v['evaluation']>=$i)
					{
						$str .= '<i class="active"></i>';
					}
					else
					{
						$str .= '<i></i>';
					}
				}
				$data[$key]['evaluation_text'] = $str;
				$data[$key]['add_time'] = date('Y-m-d H:i:s',$v['add_time']);
				$data[$key]['from_user_name'] = hide_account($v['from_user_name']);

			}
			$cache_server->set($key, $data, 1800);
		}
		return $data;
	}

	/* 赋值商品评论 */
	function _assign_goods_comment($data)
	{
		$this->assign('goods_comments', $data['comments']);
		$this->assign('page_info',      $data['page_info']);
		$this->assign('more_comments',  $data['more_comments']);
	}

	/* 取得商品咨询 */
	function _get_goods_qa($goods_id,$num_per_page)
	{
		$page = $this->_get_page($num_per_page);
		$goods_qa = & m('goodsqa');
		$qa_info = $goods_qa->find(array(
		'join' => 'belongs_to_user',
		'fields' => 'member.user_name,question_content,reply_content,time_post,time_reply',
		'conditions' => '1 = 1 AND item_id = '.$goods_id . " AND type = 'goods'",
		'limit' => $page['limit'],
		'order' =>'time_post desc',
		'count' => true
		));
		$page['item_count'] = $goods_qa->getCount();
		$this->_format_page($page);

		//如果登陆，则查出email
		if (!empty($_SESSION['user_info']))
		{
			$user_mod = & m('member');
			$user_info = $user_mod->get(array(
			'fields' => 'email',
			'conditions' => '1=1 AND user_id = '.$_SESSION['user_info']['user_id']
			));
			extract($user_info);
		}

		return array(
		'email' => $email,
		'page_info' => $page,
		'qa_info' => $qa_info,
		);
	}

	/* 赋值商品咨询 */
	function _assign_goods_qa($data)
	{
		$this->assign('email',      $data['email']);
		$this->assign('page_info',  $data['page_info']);
		$this->assign('qa_info',    $data['qa_info']);
	}

	/* 更新浏览次数 */
	function _update_views($id)
	{
		$redis = redis_server();
		$redis->hIncrBy("www.goods.view", $id, 1);
		//        $goodsstat_mod =& m('goodsstatistics');
		//        $goodsstat_mod->edit($id, "views = views + 1");
	}

	/**
     * 取得当前位置
     *
     * @param int $cate_id 分类id
     */
	function _get_curlocal($cate_id)
	{
		$parents = array();
		if ($cate_id)
		{
			$gcategory_mod =& bm('gcategory');
			$parents = $gcategory_mod->get_ancestor($cate_id, true);
		}

		$curlocal = array(
		//array('text' => LANG::get('all_categories'), 'url' => url('app=category')),
		);
		foreach ($parents as $category)
		{
			$curlocal[] = array('text' => $category['cate_name'], 'url' => url('app=search&cate_id=' . $category['cate_id']));
		}
		$curlocal[] = array('text' => LANG::get('goods_detail'));

		return $curlocal;
	}

	function _get_share($goods)
	{
		$m_share = &af('share');
		$shares = $m_share->getAll();
		$shares = array_msort($shares, array('sort_order' => SORT_ASC));
		$goods_name = ecm_iconv(CHARSET, 'utf-8', $goods['goods_name']);
		$goods_url = urlencode(SITE_URL . '/' . str_replace('&amp;', '&', url('app=goods&id=' . $goods['goods_id'])));
		$site_title = ecm_iconv(CHARSET, 'utf-8', Conf::get('site_title'));
		$share_title = urlencode($goods_name . '-' . $site_title);
		foreach ($shares as $share_id => $share)
		{
			$shares[$share_id]['link'] = str_replace(
			array('{$link}', '{$title}'),
			array($goods_url, $share_title),
			$share['link']);
		}
		return $shares;
	}

	function _get_seo_info($data)
	{
		$seo_info = array();
		$title = "";
		if ($data['cate_id'])
		{
			$gcategory_mod =& bm('gcategory');
			$parents = array_reverse($gcategory_mod->get_ancestor($data['cate_id'], true));
			foreach ($parents as $key => $cate)
			$title ? $title .= '-'.$cate['cate_name'] : $title = $cate['cate_name'];
			$seo_info['title'] = $data['goods_name'] . '-' . $title;
		}else
		$seo_info['title'] = $data['goods_name'] . '-' . Lang::get('site_title');
		//        $keywords = array(
		//            $data['brand'],
		//            $data['goods_name'],
		//            $data['cate_name']
		//        );
		//        $seo_info['keywords'] = implode(',', array_merge($keywords, $data['tags']));
		//        $seo_info['description'] = sub_str(strip_tags($data['description']), 10, true);
		$seo_info['keywords'] = str_replace('-', ',', $title);
		$seo_info['description'] = $data['goods_name'];
		return $seo_info;
	}

	/**
     * @author    yanghj  2014.6.13
     * 取得店铺6个商品分类放到导航中
     */
	function _get_store_gcategory_nav()
	{
		$gcategory_mod =& bm('gcategory', array('_store_id' => $this->_store_id));
		$gcategories = $gcategory_mod->get_list(0, true);
		if(count($gcategories)>6)
		{
			$gcategories = array_slice($gcategories, 0, 6 ,true);

		}
		return $gcategories;
	}
	/**
     * @author    yanghj  2014.6.27
     * 取得地区的省份与城市
     */
	function _get_region_list()
	{
		$arr = array();
		$str = '';
		$region_mod =& m('region');
		$arr = $region_mod->get_list(1);
		foreach($arr as $key => $val)
		{
			$arr[$key]['children'] = $region_mod->get_list($val['region_id']);
		}
		$row = array_chunk($arr, 5, true);
		foreach($row as $key => $val)
		{
			$str .= '<ul>';
			foreach($val as $k => $v)
			{
				$str .= '<li class="wrap_li"><span region_id="'.$v['region_id'].'" >'.$v['region_name'].'</span></li>';
			}
			$str .= '</ul><div class="city">';
			foreach($val as $k => $v)
			{
				$str .= '<ul>';
				foreach($v['children'] as $child)
				{
					$str .= '<li region_id="'.$child['region_id'].'">'.$child['region_name'].'</li>';
				}
				$str .= '</ul>';
			}
			$str .= '</div>';
		}

		return $str;
	}

	/**
     * @author    libaoling  2014.10.30
     * 去掉地区中的中国和区
     */

	function _rebuild_region($region)
	{
		$result = $region;
		$removeArr = array("中国");
		$regionArr = explode("\t", trim($region));
		$temp = array_pop($regionArr);
		if (strpos($temp, "区") !== false)
		{
			$removeArr[] = $temp;
		}
		$result = trim(str_replace($removeArr,'',$region));
		return $result;
	}

	/**
     * @author    yanghj  2014.12.30
     * 取得商品标签在详情图片上打上对应的标签
     */
	function _get_goods_label($arr)
	{
		$str = '';
		if($arr['is_cheap'] == 1)
		{
			$str = 'cheap';
			return $str;
		}
		if($arr['is_feature'] == 1)
		{
			$str = 'feature';
			return $str;
		}
		if($arr['is_hot'] == 1)
		{
			$str = 'hot';
			return $str;
		}

		return $str;
	}

    /**
     * @author    yanghj  2015.03.09
     * 取得商品相关联的数据
     */
    function _get_related_data($goods_id,$store_id)
    {
        $data = array();
        $goods_mod =& bm('goods', array('_store_id' => $store_id));
        $category_goods_table  = DB_PREFIX . 'category_goods';
        //获取店铺最新商品
        $data['new_goods'] = $goods_mod->find(array(
            'conditions' => "closed = 0 AND if_show = 1  AND goods_id != {$goods_id}",
            'fields'     => 'goods_name, default_image, price',
            'order'      => 'add_time desc',
            'limit'      => 8,
        ));

        if(empty($data['new_goods']))
        {
            return false;
        }
        //获取店铺推荐商品
        $data['recommended_goods'] = $goods_mod->find(array(
            'conditions' => "closed = 0 AND if_show = 1 AND recommended = 1 AND goods_id != {$goods_id}",
            'fields'     => 'goods_name, default_image, price',
            'order'      => 'add_time desc',
            'limit'      => 8,
        ));

        //获取店铺热销商品
        $data['hot_sales_goods'] = $goods_mod->find(array(
            'conditions' => "closed = 0 AND if_show = 1  AND g.goods_id != {$goods_id}",
            'fields'     => 'goods_name, default_image, price',
            'join'           => 'has_goodsstatistics',
            'order'      => 'sales desc,add_time desc',
            'limit'      => 8,
        ));

        $sql = " SELECT DISTINCT(a.goods_id) FROM " . $category_goods_table . " AS a, " . $category_goods_table . " AS b " .  "WHERE b.cate_id = a.cate_id AND b.goods_id = " . $goods_id ." AND a.goods_id !=" . $goods_id ;
        $ext_goods = $goods_mod->db->getColCached($sql);
        //获取同分类商品
        $data['same_category_goods'] = $goods_mod->find(array(
            'conditions' => "closed = 0 AND if_show = 1  AND g.goods_id ".db_create_in($ext_goods),
            'fields'     => 'goods_name, default_image, price',
            'join'           => 'has_goodsstatistics',
            'order'      => 'views desc,add_time desc',
            'limit'      => 50,
        ));

        //获取不同分类商品
        $goods_ids = implode(',',$ext_goods).','.$goods_id;
        $data['out_category_goods'] = $goods_mod->find(array(
            'conditions' => "closed = 0 AND if_show = 1  AND g.goods_id NOT IN('{$goods_ids}')",
            'fields'     => 'goods_name, default_image, price',
            'join'           => 'has_goodsstatistics',
            'order'      => 'views desc,add_time desc',
            'limit'      => 20,
        ));

        return $data;
    }

    /**
     * @author    yanghj  2015.03.03
     * 取得店铺展厅的图片
     */
    function _get_store_showroom_img($store_id)
    {
        $arr = array();
        $store_id = intval($store_id);
        for($i=1;$i<=3;$i++)
        {
            $showroom_img = '/data/files/showroom/store_'.$store_id.'/images/d_0'.$i.'.jpg';
            if(file_exists(ROOT_PATH.$showroom_img))
            {
                $arr[$i] = site_url().$showroom_img;
            }

        }

        return $arr;
    }

	/**
     * @author    censhuixing  2015.02.10
     * 取得店铺下的发货通知的图片
     */
	function _get_store_fh_img($store_id)
	{
		$store_id = intval($store_id);
		$str = 9;
		$data = $this->_get_store_ids();

		foreach ($data as $key => $store_ids)
		{
			if(in_array($store_id, $store_ids)){
				$str = $key;
				break;
			}
		}
		//$str = empty($str)?9:$str;

		$fh_img = site_url().'/data/fh/'.$str.'.jpg';
		return $fh_img;
	}

	function _get_store_ids()
	{
		return array(
		9 => array(418, 426, 428, 432, 437, 439, 443, 447, 449, 460, 461, 471, 94, 95, 97, 98, 100, 101, 106, 107, 112, 113, 118, 123, 136, 147, 179, 182, 185),
		10 => array(217, 427, 453, 472, 110, 119, 121, 134, 153, 170, 177, 210, 233),
		11 => array(132, 180, 218, 240),
		12 => array(444, 473, 103, 116, 124, 138, 144, 163, 181, 192, 203, 213),
		13 => array(452, 212, 458, 92, 105, 125, 154, 183, 211),
		14 => array(413, 102, 109, 111, 114, 129, 133, 152, 188, 215, 231),
		15 => array(87, 91, 108, 117, 120, 140, 201, 219, 236, 241),
		16 => array(90, 99, 130, 131, 139, 465, 104)
		);
	}



}

?>
